home *** CD-ROM | disk | FTP | other *** search
/ Freaks Macintosh Archive / Freaks Macintosh Archive.bin / Freaks Macintosh Archives / Textfiles / zines / Phrack / Phrack Issue 52.sit / Phrack52 / 52 / P52-20 < prev   
Text File  |  1998-01-27  |  7KB  |  270 lines

  1. ---[  Phrack Magazine   Volume 8, Issue 52 January 26, 1998, article 20 of 20
  2.  
  3.  
  4. -------------------------[  Phrack Magzine Extraction Utility
  5.  
  6.  
  7. --------[  Phrack Staff
  8.  
  9.  
  10.     Added to the list of extraction variants this time is a version in AWK,
  11.     and a version in sh.  Also, the C version has ben spruced up to accept
  12.     file name globs.  Keep `em coming...
  13.  
  14.  
  15. ---------------------8<------------CUT-HERE----------->8---------------------
  16.  
  17. <++> PEU/extract2.c 
  18. /*  extract.c by Phrack Staff and sirsyko 
  19.  *
  20.  *  (c) Phrack Magazine, 1997 
  21.  *      1.8.98 rewritten by route:
  22.  *          - aesthetics
  23.  *          - now accepts file globs
  24.  *      todo:
  25.  *          - more info in tag header (file mode, checksum)
  26.  *  Extracts textfiles from a specially tagged flatfile into a hierarchical 
  27.  *  directory strcuture.  Use to extract source code from any of the articles 
  28.  *  in Phrack Magazine (first appeared in Phrack 50).
  29.  *
  30.  *  gcc -o extract extract.c
  31.  *  
  32.  *  ./extract file1 file2 file3 ...
  33.  */
  34.  
  35.  
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <sys/stat.h>
  39. #include <string.h>
  40. #include <dirent.h>
  41.  
  42. #define BEGIN_TAG   "<++> "
  43. #define END_TAG     "<-->"
  44. #define BT_SIZE     strlen(BEGIN_TAG)
  45. #define ET_SIZE     strlen(END_TAG)
  46.  
  47. struct f_name
  48. {
  49.     u_char name[256];
  50.     struct f_name *next;
  51. };
  52.  
  53. int
  54. main(int argc, char **argv)
  55.     u_char b[256], *bp, *fn;
  56.     int i, j = 0;
  57.     FILE *in_p, *out_p = NULL; 
  58.     struct f_name *fn_p = NULL, *head = NULL; 
  59.  
  60.     if (argc < 2)
  61.     {
  62.         printf("Usage: %s file1 file2 ... filen\n", argv[0]);
  63.         exit(0); 
  64.     }
  65.  
  66.     /*
  67.      *  Fill the f_name list with all the files on the commandline (ignoring
  68.      *  argv[0] which is this executable).  This includes globs.
  69.      */
  70.     for (i = 1; (fn = argv[i++]); )
  71.     {
  72.         if (!head)
  73.         {
  74.             if (!(head = (struct f_name *)malloc(sizeof(struct f_name))))
  75.             {
  76.                 perror("malloc");
  77.                 exit(1);
  78.             }
  79.             strncpy(head->name, fn, sizeof(head->name));
  80.             head->next = NULL;
  81.             fn_p = head;
  82.         }
  83.         else
  84.         {
  85.             if (!(fn_p->next = (struct f_name *)malloc(sizeof(struct f_name))))
  86.             {
  87.                 perror("malloc");
  88.                 exit(1);
  89.             }
  90.             fn_p = fn_p->next;
  91.             strncpy(fn_p->name, fn, sizeof(fn_p->name));
  92.             fn_p->next = NULL;
  93.         }
  94.     }
  95.     /*
  96.      *  Sentry node.
  97.      */
  98.     if (!(fn_p->next = (struct f_name *)malloc(sizeof(struct f_name))))
  99.     {
  100.         perror("malloc");
  101.         exit(1);
  102.      }
  103.     fn_p = fn_p->next;
  104.     fn_p->next = NULL;
  105.  
  106.     /*
  107.      *  Check each file in the f_name list for extraction tags.
  108.      */
  109.     for (fn_p = head; fn_p->next; fn_p = fn_p->next)
  110.     {
  111.         if (!(in_p = fopen(fn_p->name, "r")))
  112.         {
  113.             fprintf(stderr, "Could not open input file %s.\n", fn_p->name);
  114.         continue;
  115.         }
  116.         else fprintf(stderr, "Opened %s\n", fn_p->name);
  117.         while (fgets(b, 256, in_p))
  118.         { 
  119.             if (!strncmp (b, BEGIN_TAG, BT_SIZE))
  120.             { 
  121.             b[strlen(b) - 1] = 0;           /* Now we have a string. */
  122.                 j++;
  123.  
  124.                 if ((bp = strchr(b + BT_SIZE + 1, '/')))
  125.                 {
  126.                     while (bp)
  127.                     {
  128.                 *bp = 0;
  129.                 mkdir(b + BT_SIZE, 0700); 
  130.                 *bp = '/';
  131.                 bp = strchr(bp + 1, '/'); 
  132.             }
  133.                 }
  134.                 if ((out_p = fopen(b + BT_SIZE, "w")))
  135.                 {
  136.                     printf("- Extracting %s\n", b + BT_SIZE);
  137.                 }
  138.                 else
  139.                 {
  140.             printf("Could not extract '%s'.\n", b + BT_SIZE);
  141.             continue;
  142.             }
  143.         } 
  144.             else if (!strncmp (b, END_TAG, ET_SIZE))
  145.             {
  146.             if (out_p) fclose(out_p);
  147.             else
  148.                 {
  149.                 fprintf(stderr, "Error closing file %s.\n", fn_p->name);
  150.             continue;
  151.             }
  152.             } 
  153.             else if (out_p)
  154.             {
  155.                 fputs(b, out_p);
  156.             }
  157.         }
  158.     }
  159.     if (!j) printf("No extraction tags found in list.\n");
  160.     else printf("Extracted %d file(s).\n", j);
  161.     return (0);
  162. }
  163.  
  164. /* EOF */
  165. <-->
  166. <++> PEU/extract.pl
  167. # Daos <daos@nym.alias.net>
  168. #!/bin/sh -- # -*- perl -*- -n
  169. eval 'exec perl $0 -S ${1+"$@"}' if 0;
  170.  
  171. $opening=0;
  172.  
  173. if (/^\<\+\+\>/) {$curfile = substr($_ , 5); $opening=1;};
  174. if (/^\<\-\-\>/) {close ct_ex; $opened=0;}; 
  175. if ($opening) {                        
  176.         chop $curfile;                 
  177.         $sex_dir= substr( $curfile, 0, ((rindex($curfile,'/'))) ) if ($curfile =~ m/\//);
  178.         eval {mkdir $sex_dir, "0777";}; 
  179.         open(ct_ex,">$curfile"); 
  180.         print "Attempting extraction of $curfile\n";
  181.         $opened=1; 
  182. }
  183. if ($opened && !$opening) {print ct_ex $_}; 
  184. <-->
  185.  
  186. <++> PEU/extract.awk
  187. #!/usr/bin/awk -f
  188. #
  189. # Yet Another Extraction Script
  190. # - <sirsyko>
  191. #
  192. /^\<\+\+\>/ {
  193.         ind = 1
  194.         File = $2
  195.         split ($2, dirs, "/")
  196.         Dir="."
  197.         while ( dirs[ind+1] ) {
  198.                 Dir=Dir"/"dirs[ind]
  199.                 system ("mkdir " Dir" 2>/dev/null")
  200.                 ++ind
  201.         }
  202.         next
  203. }
  204. /^\<\-\-\>/ {
  205.         File = ""
  206.         next
  207. }
  208. File { print >> File }
  209. <-->
  210.  
  211. <++> PEU/extract.sh
  212. #!/bin/sh
  213. # exctract.sh : Written 9/2/1997 for the Phrack Staff by <sirsyko>
  214. #
  215. # note, this file will create all directories relative to the current directory
  216. # originally a bug, I've now upgraded it to a feature since I dont want to deal
  217. # with the leading / (besides, you dont want hackers giving you full pathnames
  218. # anyway, now do you :)
  219. # Hopefully this will demonstrate another useful aspect of IFS other than 
  220. # haxoring rewt
  221. #
  222. # Usage: ./extract.sh <filename>
  223.  
  224. cat $* | (
  225. Working=1
  226. while [ $Working ];
  227. do
  228.         OLDIFS1="$IFS"
  229.         IFS=
  230.         if read Line; then
  231.                 IFS="$OLDIFS1"
  232.                 set -- $Line
  233.                 case "$1" in
  234.                 "<++>") OLDIFS2="$IFS"
  235.                         IFS=/
  236.                         set -- $2
  237.                         IFS="$OLDIFS2"
  238.                         while [ $# -gt 1 ]; do
  239.                                 File=${File:-"."}/$1
  240.                                 if [ ! -d $File ]; then
  241.                                         echo "Making dir $File"
  242.                                         mkdir $File
  243.                                 fi
  244.                                 shift
  245.                         done                               
  246.                         File=${File:-"."}/$1
  247.                         echo "Storing data in $File"
  248.                 ;;
  249.                 "<-->") if [ "x$File" != "x" ]; then
  250.                                 unset File
  251.                         fi ;;
  252.                 *)      if [ "x$File" != "x" ]; then
  253.                                         IFS=
  254.                                         echo "$Line" >> $File
  255.                                         IFS="$OLDIFS1"
  256.                         fi
  257.                 ;;
  258.                 esac
  259.                 IFS="$OLDIFS1"
  260.         else
  261.                 echo "End of file"
  262.                 unset Working
  263.         fi
  264. done
  265. )                                                                    
  266. <-->
  267.  
  268. ----[  EOF
  269.